﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Resources;
using System.Globalization;
using System.IO;

using TrainingCenterSystem;
using TrainingCenterSystem.UserManagement;
using TrainingCenterSystem.TestManagement;
using TrainingCenterSystem.VocabularyManagement;

namespace TrainingCenterWinClient.Forms
{
    public partial class TestExecutionForm : Form
    {
        #region private members
        private TrainingCenter _trainingCenter;
        private ResourceManager _resMgr;
        private VocabularyTraining _vocTraining;
        private IndividualTest _individualTest;
        private List<GenericWord> _sources;
        private int _currentQuestionIndex;
        private string _confirmSubmit;
        private string _confirmCancel;
        private bool _cancelled;
        #endregion

        #region constructors
        /// <summary>
        /// Creates a form for test execution
        /// </summary>
        /// <param name="trainingCenter">the training center</param>
        /// <param name="vocTraining">the vocabulary training</param>
        public TestExecutionForm(TrainingCenter trainingCenter, VocabularyTraining vocTraining,List<GenericWord> sources)
        {
            InitializeComponent();
            _trainingCenter = trainingCenter;
            _vocTraining = vocTraining;
            _sources = sources;

            this.initForm();
            this.startTest();
        }
        #endregion

        #region methods
        /// <summary>
        /// Initializes the form
        /// </summary>
        private void initForm()
        {
            // init language
            this.UpdateLanguage(_trainingCenter.CurrentLanguageID);

            _cancelled = true;
        }

        /// <summary>
        /// starts the test
        /// </summary>
        private void startTest()
        {
            // create an individual test instance
            this.createIndividualTest();
            _currentQuestionIndex = 0;
            this.showCurrentQuestion();

        }        

        /// <summary>
        /// Creates an instance of IndividualTest
        /// </summary>
        private void createIndividualTest()
        {
            _individualTest = new IndividualTest(_vocTraining.TestID, _trainingCenter.CurrentUser, _vocTraining.TestQuestions);

        }

        /// <summary>
        /// Shows the current question
        /// </summary>
        private void showCurrentQuestion()
        {
            this.showQuestionNumber();
            this.txtAnswer.Text = "";
            this.toggleButtons();
            TestQuestion aQuestion = _vocTraining.TestQuestions[_currentQuestionIndex];

            if (aQuestion is FreeTextQuestion)
            {
                FreeTextQuestion freeText = (FreeTextQuestion)aQuestion;
                this.lblQuestion.Text = freeText.WordString;                
            }
        }

        /// <summary>
        /// Shows the number of the current question
        /// </summary>
        private void showQuestionNumber()
        {
            this.lblCurrentQuestionOutOfTotal.Text = (_currentQuestionIndex + 1).ToString() + "/" + _vocTraining.TestQuestions.Count.ToString();
            
        }

        /// <summary>
        /// Activates and deactivates the buttons 
        /// </summary>
        private void toggleButtons()
        {
            if (_currentQuestionIndex == 0)
            {
                this.btnPreviousQuestion.Enabled = false;
                return;
            }
            else if (_currentQuestionIndex == _vocTraining.TestQuestions.Count-1)
            {
                this.btnNextQuestion.Enabled = false;
                return;
            }

            this.btnPreviousQuestion.Enabled = true;
            this.btnNextQuestion.Enabled = true;
        }
        

        /// <summary>
        /// Updates the language of the UI
        /// </summary>
        /// <param name="langID">the language-id</param>
        private void UpdateLanguage(string langID)
        {
            try
            {
                // Set Language
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(langID);

                // Init ResourceManager
                _resMgr =
                        new ResourceManager("TrainingCenterWinClient.ReStrings",
                        typeof(TestExecutionForm).Assembly);

                // assign localized strings to UI                
                this.Text = _resMgr.GetString("TestExecutionForm_caption");
                this.lblAnswerText.Text = _resMgr.GetString("TestExecutionForm_lblAnswerText");
                this.lblQuestionText.Text = _resMgr.GetString("TestExecutionForm_lblQuestionText");
                this.btnPreviousQuestion.Text = _resMgr.GetString("TestExecutionForm_btnPrevQuestion");
                this.btnNextQuestion.Text = _resMgr.GetString("TestExecutionForm_btnNextQuestion");
                this.btnSubmit.Text = _resMgr.GetString("TextExecutionForm_btnSubmit");

                _confirmSubmit = _resMgr.GetString("TestExecutionForm_confirmSubmit");
                _confirmCancel = _resMgr.GetString("TestExecutionForm_confirmCancel");                
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// Handles the click on the Next-button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnNextQuestion_Click(object sender, EventArgs e)
        {
            this.setAnswer();
            _currentQuestionIndex++;
            this.showCurrentQuestion();
        }

        /// <summary>
        /// Handles the click on the Previous-button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPreviousQuestion_Click(object sender, EventArgs e)
        {
            this.setAnswer();
            _currentQuestionIndex--;
            this.showCurrentQuestion();
        }

        private void setAnswer()
        {
            string answerText = this.txtAnswer.Text;
            if (answerText == null)
                answerText = "";
            _individualTest.AddAnswer(_currentQuestionIndex.ToString(), answerText);
            //_individualTest.TestAnswers[_currentQuestionIndex].AnswerText = answerText;
        }

        /// <summary>
        /// Handles the click on the Submit-button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSubmit_Click(object sender, EventArgs e)
        {
            this.setAnswer();

            // ask for confirmation to submit the test
            DialogResult result = MessageBox.Show(_confirmSubmit, "", MessageBoxButtons.YesNo);            
            if (result == DialogResult.Yes)
            {
                _cancelled = false;               
                TestEvaluator eval = new TestEvaluator(_individualTest, _sources);
                TestResultForm resultForm = new TestResultForm(_trainingCenter, _individualTest);                
                resultForm.ShowDialog();
                this.Hide();
            }
            else 
            {
                return;
            }
        }

        /// <summary>
        /// Handles the closing-event
        /// </summary>
        /// <param name="e"></param>
        protected override void OnClosing(CancelEventArgs e)
        {
            if (_cancelled)
            {

                DialogResult result = MessageBox.Show(_confirmCancel, "", MessageBoxButtons.YesNo);
                if (result == DialogResult.No)
                    return;
            }
            base.OnClosing(e);
        }        

        private void lblCurrentQuestionOutOfTotal_Click(object sender, EventArgs e)
        {

        }

        private void TestExecutionForm_Load(object sender, EventArgs e)
        {

        }
        #endregion
    }
}
